//
//  SPMineViewController.m
//  Sport
//
//  Created by Cass on 2017/12/2.
//  Copyright © 2017年 Cass. All rights reserved.
//

#import "SPMineViewController.h"
#import "SPMyInformationViewController.h"
#import "SPStateViewController.h"
#import "SPOrderViewController.h"
#import "SPCollectViewController.h"
#import "SPIdeaViewController.h"
#import "SPSetupViewController.h"
#import "SPEditViewController.h"
#import "SPFriendCell.h"
#import "SPLoginViewController.h"
@interface SPMineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) UIButton *noticeButton;
@property (weak, nonatomic) UIButton *stateButton;
@property (weak, nonatomic) UIButton *fansBUtton;

@end

@implementation SPMineViewController

- (UIButton *)noticeButton{
    if (!_noticeButton) {
        _noticeButton = [[UIButton alloc]init];
        [_noticeButton addTarget:self action:@selector(toState) forControlEvents:UIControlEventTouchUpInside];
    }
    return _noticeButton;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)toState{
    SPStateViewController *stateVC = [[UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPStateViewController class])];
    stateVC.title = @"我的动态";
    [self.navigationController pushViewController:stateVC animated:YES];
}
#pragma mark - UITableViewDelegate -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"You Selected %@",[tableView cellForRowAtIndexPath:indexPath].textLabel.text);
    if (indexPath.section == 0) {
        SPEditViewController *editVC = [[UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPEditViewController class])];
        editVC.title = @"编辑资料";
        [self.navigationController pushViewController:editVC animated:YES];
    }else if (indexPath.section == 1){
        [self.stateButton addTarget:self action:@selector(toState) forControlEvents:UIControlEventTouchUpInside];

    }else{
        if (indexPath.row == 0) {
            SPOrderViewController *orderVC = [[UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPOrderViewController class])];
            orderVC.title = @"订单";
            [self.navigationController pushViewController:orderVC animated:YES];
        }
       else if (indexPath.row == 1) {
            SPCollectViewController *collectVC = [[UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPCollectViewController class])];
            collectVC.title = @"收藏";
            [self.navigationController pushViewController:collectVC animated:YES];
        }
        else if (indexPath.row == 2) {
            SPIdeaViewController *ideaVC = [[UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPIdeaViewController class])];
            ideaVC.title = @"意见反馈";
            [self.navigationController pushViewController:ideaVC animated:YES];
        }else if (indexPath.row == 3){
            SPSetupViewController *setupVC = [[UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPSetupViewController class])];
            setupVC.title = @"设置";
            [self.navigationController pushViewController:setupVC animated:YES];
        }else if (indexPath.row == 4){
            [SPHUDUtil showHUDWithDismissTimeInterval:1 message:@"退出成功" hudType:SPHUDTypeSuccess];
            NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
            [userDefault removeObjectForKey:@"phoneNumber"];
            [userDefault removeObjectForKey:@"password"];
            [userDefault synchronize];
            
            dispatch_time_t timer = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC));
            dispatch_after(timer, dispatch_get_main_queue(), ^{
                SPLoginViewController *loginVC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPLoginViewController class])];
                [self presentViewController:loginVC animated:YES completion:nil];
//            [self.navigationController pushViewController:loginVC animated:YES];
            });

        }
    }
}

#pragma mark - UITableViewDataSource -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0) {
        return 1;
    }else if (section == 1){
        return 1;
    }else{
        return 5;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if (indexPath.section == 0) {
        cell.textLabel.text = @"我";
        cell.imageView.image = [UIImage imageNamed:@"ha"];
    }else if (indexPath.section == 1){
//        if (indexPath.row == 0) {
//            cell.textLabel.text = @"我的动态";
//        }else if (indexPath.row == 1){
//            cell.textLabel.text = @"收藏";
//        }
        SPFriendCell *friendCell = [SPFriendCell cellWithTableView:tableView];
        friendCell.stateBtn = self.stateButton;
        self.noticeButton = friendCell.noticeBtn;
        self.fansBUtton = friendCell.noticeBtn;
        cell = friendCell;
    }else{
        if (indexPath.row == 0) {
            cell.textLabel.text = @"订单";
        }
        else if (indexPath.row ==1) {
            cell.textLabel.text = @"收藏";
        }
        else if (indexPath.row == 2) {
            cell.textLabel.text = @"意见反馈";
        }else if (indexPath.row == 3){
            cell.textLabel.text = @"设置";
        }else{
            cell.textLabel.text = @"退出";
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//    if (section == 0) {
//        return @" ";
//    }else if (section == 1){
//        return @" ";
//    }else{
//        return @" ";
//    }
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 87.0f;
    }else{
        return 57.0f;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10.0f;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
